package com.jprocms.module.cms.service.sitedetailtpl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jprocms.module.cms.api.dto.SiteContentDefTplDto;
import com.jprocms.module.cms.api.dto.SiteDefTplDto;
import com.jprocms.module.cms.api.dto.SiteDefTplQueryDto;
import com.jprocms.module.cms.convert.sitedetailtpl.SiteDetailTplConvert;
import com.jprocms.module.cms.dal.dataobject.sitedetailtpl.SiteDetailTplDO;
import com.jprocms.module.cms.dal.mysql.sitedetailtpl.SiteDetailTplMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.SITE_DETAIL_TPL_NOT_EXISTS;

/**
 * 站点各个发布平台的内容详情默认模板 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class SiteDetailTplServiceImpl implements SiteDetailTplService {

    @Resource
    private SiteDetailTplMapper siteDetailTplMapper;

    @Override
    public Long createSiteDetailTpl(SiteContentDefTplDto createReqVO) {
        // 插入
        SiteDetailTplDO siteDetailTpl = SiteDetailTplConvert.INSTANCE.convert(createReqVO);
        siteDetailTplMapper.insert(siteDetailTpl);
        // 返回
        return siteDetailTpl.getId();
    }

    @Override
    public void updateSiteDetailTpl(SiteContentDefTplDto updateReqVO) {
        // 校验存在
        validateSiteDetailTplExists(updateReqVO.getId());
        // 更新
        SiteDetailTplDO updateObj = SiteDetailTplConvert.INSTANCE.convert(updateReqVO);
        siteDetailTplMapper.updateById(updateObj);
    }

    @Override
    public void deleteSiteDetailTpl(Long id) {
        // 校验存在
        validateSiteDetailTplExists(id);
        // 删除
        siteDetailTplMapper.deleteById(id);
    }

    private void validateSiteDetailTplExists(Long id) {
        if (siteDetailTplMapper.selectById(id) == null) {
            throw exception(SITE_DETAIL_TPL_NOT_EXISTS);
        }
    }

    @Override
    public SiteDetailTplDO getSiteDetailTpl(Long id) {
        return siteDetailTplMapper.selectById(id);
    }

    @Override
    public List<SiteDetailTplDO> getSiteDetailTplList(Collection<Long> ids) {
        return siteDetailTplMapper.selectBatchIds(ids);
    }

    @Override
    public void createOrUpdateSiteDetailTpls(SiteDefTplDto siteDefTplDto) {
        Set<SiteContentDefTplDto> contentDefTpls = siteDefTplDto.getContentDefTpls();
        if (CollUtil.isNotEmpty(contentDefTpls)) {
            for (SiteContentDefTplDto siteContentDefTplDto : contentDefTpls) {
                SiteDetailTplDO siteDetailTplDO = selectSiteContentTpl(siteDefTplDto, siteContentDefTplDto);
                siteContentDefTplDto.setSiteId(siteDefTplDto.getSiteId());
                siteContentDefTplDto.setPlatformId(siteDefTplDto.getPlatformId());
                if (siteDetailTplDO != null) {
                    siteContentDefTplDto.setId(siteDetailTplDO.getId());
                    updateSiteDetailTpl(siteContentDefTplDto);
                } else {
                    createSiteDetailTpl(siteContentDefTplDto);
                }
            }
        }
    }

    @Override
    public List<SiteDetailTplDO> selectSiteTpl(SiteDefTplQueryDto siteDefTplQueryDto) {
        LambdaQueryWrapper<SiteDetailTplDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SiteDetailTplDO::getSiteId, siteDefTplQueryDto.getSiteId());
        queryWrapper.eq(SiteDetailTplDO::getPlatformId, siteDefTplQueryDto.getPlatformId());
        return siteDetailTplMapper.selectList(queryWrapper);
    }

    public SiteDetailTplDO selectSiteContentTpl(SiteDefTplDto siteDefTplDto, SiteContentDefTplDto siteContentDefTplDto) {
        LambdaQueryWrapper<SiteDetailTplDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SiteDetailTplDO::getSiteId, siteDefTplDto.getSiteId());
        queryWrapper.eq(SiteDetailTplDO::getPlatformId, siteDefTplDto.getPlatformId());
        queryWrapper.eq(SiteDetailTplDO::getModelId, siteContentDefTplDto.getModelId());
        return siteDetailTplMapper.selectOneByNoError(queryWrapper);
    }

}
